package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.entity.dto.SubmitDto;
import cn.edu.lingnan.entity.Team;
import cn.edu.lingnan.entity.TeamMember;
import cn.edu.lingnan.mapper.TeamMapper;
import cn.edu.lingnan.mapper.TeamMemberMapper;
import cn.edu.lingnan.service.ITeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Override
    public Team teamSave(SubmitDto submitDto) {
            Team team = new Team();

            team.setTeamName(submitDto.getTeamName());
            team.setAward(submitDto.getAward());
            team.setContestId(submitDto.getContestId());
            team.setTeamLeaderId(submitDto.getTeamLeader().getTeamNumberId());
            team.setTeamGuideId(submitDto.getTeamGuide().getTeamNumberId());
            team.setTeamMember1Id(submitDto.getTeamNumber1().getTeamNumberId());
            team.setTeamMember2Id(submitDto.getTeamNumber2().getTeamNumberId());
            team.setTeamMember3Id(submitDto.getTeamNumber3().getTeamNumberId());
            team.setTeamMember4Id(submitDto.getTeamNumber4().getTeamNumberId());
            team.setStatus("待审批");
            System.out.println(team);
            LocalDateTime currentTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String teamId = currentTime.format(formatter);
            team.setTeamId(teamId);
            teamMapper.insert(team);
            //插入成功后进入第二张表

            TeamMember teamMember = new TeamMember();
            teamMember.setTeam_id(team.getTeamId());
            //ID
            teamMember.setTeam_leader_id(team.getTeamLeaderId());
            teamMember.setTeam_guide_id(team.getTeamGuideId());
            teamMember.setTeam_member1_id(team.getTeamMember1Id());
            teamMember.setTeam_member2_id(team.getTeamMember2Id());
            teamMember.setTeam_member3_id(team.getTeamMember3Id());
            teamMember.setTeam_member4_id(team.getTeamMember4Id());

            //名字
            teamMember.setTeam_leader_name(submitDto.getTeamLeader().getName());
            teamMember.setTeam_guide_name(submitDto.getTeamGuide().getName());
            teamMember.setTeam_member1_name(submitDto.getTeamNumber1().getName());
            teamMember.setTeam_member2_name(submitDto.getTeamNumber2().getName());
            teamMember.setTeam_member3_name(submitDto.getTeamNumber3().getName());
            teamMember.setTeam_member4_name(submitDto.getTeamNumber4().getName());

            //邮箱
            teamMember.setTeam_leader_email(submitDto.getTeamLeader().getEmail());
            teamMember.setTeam_guide_email(submitDto.getTeamGuide().getEmail());
            teamMember.setTeam_member1_email(submitDto.getTeamNumber1().getEmail());
            teamMember.setTeam_member2_email(submitDto.getTeamNumber2().getEmail());
            teamMember.setTeam_member3_email(submitDto.getTeamNumber3().getEmail());
            teamMember.setTeam_member4_email(submitDto.getTeamNumber4().getEmail());

            //电话
            teamMember.setTeam_leader_phone(submitDto.getTeamLeader().getPhone());
            teamMember.setTeam_guide_phone(submitDto.getTeamGuide().getPhone());
            teamMember.setTeam_member1_phone(submitDto.getTeamNumber1().getPhone());
            teamMember.setTeam_member2_phone(submitDto.getTeamNumber2().getPhone());
            teamMember.setTeam_member3_phone(submitDto.getTeamNumber3().getPhone());
            teamMember.setTeam_member4_phone(submitDto.getTeamNumber4().getPhone());

            //性别
            teamMember.setTeam_leader_gender(submitDto.getTeamLeader().getGender());
            teamMember.setTeam_guide_gender(submitDto.getTeamGuide().getGender());
            teamMember.setTeam_member1_gender(submitDto.getTeamNumber1().getGender());
            teamMember.setTeam_member2_gender(submitDto.getTeamNumber2().getGender());
            teamMember.setTeam_member3_gender(submitDto.getTeamNumber3().getGender());
            teamMember.setTeam_member4_gender(submitDto.getTeamNumber4().getGender());


            teamMember.setTeam_leader_major(submitDto.getTeamLeader().getMajor());
            teamMember.setTeam_guide_college(submitDto.getTeamGuide().getCollege());
            teamMember.setTeam_member1_major(submitDto.getTeamNumber1().getMajor());
            teamMember.setTeam_member2_major(submitDto.getTeamNumber2().getMajor());
            teamMember.setTeam_member3_major(submitDto.getTeamNumber3().getMajor());
            teamMember.setTeam_member4_major(submitDto.getTeamNumber4().getMajor());

            teamMemberMapper.insert(teamMember);

        return team;
    }
}
